import os
import django
import random
from decimal import Decimal
from datetime import datetime, timedelta
from faker import Faker

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'warehouse_system.settings')
django.setup()

from django.contrib.auth.hashers import make_password
from employee.models import Employee
from product.models import Product
from supplier.models import Supplier
from customer.models import Customer
from stock.models import (
    StockIn, StockInDetail, StockOut, StockOutDetail,
    StockCheck, StockCheckDetail, StockTransfer, StockTransferDetail,
    StockRecord
)

fake = Faker('zh_CN')

def generate_employees(count=10):
    # 创建管理员
    # Employee.objects.create(
    #     username='admin',
    #     password=make_password('123456'),
    #     name='管理员',
    #     phone='13800000000',
    #     sex='男',
    #     id_number='110101199001010011',
    #     role=1,
    #     status=1
    # )
    
    # 创建普通员工
    for _ in range(count):
        Employee.objects.create(
            username=fake.user_name(),
            password=make_password('123456'),
            name=fake.name(),
            phone=fake.phone_number(),
            sex=random.choice(['男', '女']),
            id_number=fake.ssn(),
            role=2,
            status=1
        )

def generate_products(count=20):
    units = ['个', '件', '箱', '袋', 'kg', 'g', 'L', 'ml']
    for _ in range(count):
        purchase_price = Decimal(str(random.uniform(1, 1000)))
        Product.objects.create(
            name=fake.word() + random.choice(['', '牌', '系列', '型']) + random.choice(['产品', '商品', '货物']),
            code=f'P{random.randint(1000, 9999)}',
            unit=random.choice(units),
            purchase_price=purchase_price,
            sale_price=purchase_price * Decimal('1.5'),
            stock=random.randint(0, 1000),
            warning_stock=random.randint(10, 100),
            status=1
        )

def generate_suppliers(count=10):
    for _ in range(count):
        Supplier.objects.create(
            name=fake.company() + random.choice(['有限公司', '批发部', '贸易公司']),
            contact=fake.name(),
            phone=fake.phone_number(),
            address=fake.address(),
            status=1
        )

def generate_customers(count=10):
    for _ in range(count):
        Customer.objects.create(
            name=fake.company() + random.choice(['超市', '商店', '便利店']),
            contact=fake.name(),
            phone=fake.phone_number(),
            address=fake.address(),
            status=1
        )

def generate_stock_records(count=50):
    employees = list(Employee.objects.all())
    products = list(Product.objects.all())
    suppliers = list(Supplier.objects.all())
    customers = list(Customer.objects.all())
    
    # 生成入库记录
    for i in range(count):
        stock_in = StockIn.objects.create(
            stock_in_no=f'RK{datetime.now().strftime("%Y%m%d")}{str(i+1).zfill(6)}',
            supplier=random.choice(suppliers),
            total_amount=0,
            status=random.choice([0, 1, 2]),
            create_user=random.choice(employees)
        )
        
        # 生成入库明细
        detail_count = random.randint(1, 5)
        total_amount = Decimal('0')
        for _ in range(detail_count):
            product = random.choice(products)
            quantity = random.randint(1, 100)
            price = product.purchase_price
            amount = price * quantity
            total_amount += amount
            
            StockInDetail.objects.create(
                stock_in=stock_in,
                product=product,
                price=price,
                quantity=quantity,
                amount=amount
            )
        
        stock_in.total_amount = total_amount
        stock_in.save()
    
    # 生成出库记录
    for i in range(count):
        stock_out = StockOut.objects.create(
            stock_out_no=f'CK{datetime.now().strftime("%Y%m%d")}{str(i+1).zfill(6)}',
            customer=random.choice(customers),
            total_amount=0,
            status=random.choice([0, 1, 2]),
            create_user=random.choice(employees)
        )
        
        # 生成出库明细
        detail_count = random.randint(1, 5)
        total_amount = Decimal('0')
        for _ in range(detail_count):
            product = random.choice(products)
            quantity = random.randint(1, 100)
            price = product.sale_price
            amount = price * quantity
            total_amount += amount
            
            StockOutDetail.objects.create(
                stock_out=stock_out,
                product=product,
                price=price,
                quantity=quantity,
                amount=amount
            )
        
        stock_out.total_amount = total_amount
        stock_out.save()

if __name__ == '__main__':
    print('开始生成数据...')
    generate_employees()
    print('员工数据生成完成')
    generate_products()
    print('产品数据生成完成')
    generate_suppliers()
    print('供应商数据生成完成')
    generate_customers()
    print('客户数据生成完成')
    generate_stock_records()
    print('库存记录生成完成')
    print('所有数据生成完成！') 